package com.ljm.shiro.controller;

import com.ljm.common.constants.ShiroConstants;
import com.ljm.common.dto.BaseResult;
import com.ljm.common.enums.ResultStatus;
import com.ljm.common.util.ResultUtil;
import com.ljm.shiro.dto.UserDto;
import com.ljm.shiro.util.UserUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 版权：    Copyright by ljm
 * 描述：
 * 修改人：  HuamingChen
 * 修改时间：2018/12/11
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
@RestController
public class LoginController {
    /**
     * 日志
     */
    private static final Logger LOG = LoggerFactory.getLogger(LoginController.class);

    @GetMapping(value = "/doLogin")
    @ResponseBody
    public BaseResult doLogin(String username, String password,HttpServletRequest request) {
        return this.handleLogin(username,password,request);
    }

    @PostMapping(value = "/doLogin")
    @ResponseBody
    public BaseResult login(@RequestBody UserDto userDto,HttpServletRequest request) {
        String username=userDto.getUsername();
        String password=userDto.getPassword();
        return this.handleLogin(username,password,request);
    }

    // 处理登录
    private BaseResult handleLogin(String username,String password,HttpServletRequest request){
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);
            request.getSession().setAttribute(ShiroConstants.CURRENT_LOGIN_NAME,username);
        } catch (UnknownAccountException uae) {
            LOG.info("对用户[{}]进行登录验证..验证未通过,未知账户",username);
            return ResultUtil.error(ResultStatus.FIAL.getCode(), "用户名错误,请重新输入!");
        } catch (IncorrectCredentialsException ice) {
            LOG.info("对用户[{}]进行登录验证..验证未通过,错误的凭证",username);
            return ResultUtil.error(ResultStatus.FIAL.getCode(), "密码错误,请重新输入!");
        } catch (LockedAccountException lae) {
            LOG.info("对用户[{}]进行登录验证..验证未通过,账户已锁定",username);
            return ResultUtil.error(ResultStatus.FIAL.getCode(), "账号已经被锁定,请联系管理员");
        } catch (ExcessiveAttemptsException eae) {
            LOG.info("对用户[{}]进行登录验证..验证未通过,错误次数过多",username);
            return ResultUtil.error(ResultStatus.FIAL.getCode(), eae.getMessage());
        } catch (AuthenticationException ae) {
            LOG.info("对用户[{}]进行登录验证..验证未通过,堆栈轨迹如下",username);
            return ResultUtil.error(ResultStatus.FIAL.getCode(), "用户认证失败,请重新输入账号密码登录!");
        } catch (Exception e) {
            return ResultUtil.error(ResultStatus.FIAL.getCode(), "系统异常,请联系管理员!");
        }
        String sessionId=SecurityUtils.getSubject().getSession().getId().toString();
        return ResultUtil.successData(ResultStatus.SUCCESS.getCode(),"登录成功",sessionId);
    }

    @GetMapping("/getUserInfo")
    public BaseResult getUserInfo(HttpServletRequest request){
        return ResultUtil.successData(UserUtil.getUserInfo());
    }

    @GetMapping("/unauthorized")
    public String unauthorized() {
        return "unauthorized";
    }

}
